#include "GLTextureFont2D.h"

using namespace game_engine::open_gl;
using namespace game_engine::utils;

///////////////////////////////////////////////////////////////////////////////
// Constructors and Destructors
///////////////////////////////////////////////////////////////////////////////
GLTextureFont2D::GLTextureFont2D(void)
    : GLTexture2D(), base(0), rows(0), columns(0), offset(0)
{
}

GLTextureFont2D::GLTextureFont2D(const string &filename, const INT32 type, const UINT32 ro, 
               const UINT32 col, const UINT32 pixelsWidth, 
               const UINT32 pixelsHeight, const UINT32 spacing,
               const UINT32 off)
    : GLTexture2D(filename, type), base(0), rows(ro), columns(col), offset(off)
{
    // Creating Display Lists
	base = glGenLists(rows * columns);								
	// Select Our Font Texture
    glBindTexture(GL_TEXTURE_2D, name);			

    FLOAT32 offsetS = static_cast<FLOAT32>((pixelsWidth*1.0f) / (pixelsWidth*columns));
    FLOAT32 offsetT = static_cast<FLOAT32>((pixelsHeight*1.0f) / (rows*pixelsHeight));

	for (UINT32 loop=0; loop<(rows * columns); loop++)
	{
        // X Position Of Current Character
		FLOAT32 cx = static_cast<FLOAT32>((loop%rows) / (pixelsWidth*1.0f));
        // Y Position Of Current Character
		FLOAT32 cy = static_cast<FLOAT32>((loop/columns) / (pixelsHeight*1.0f));						

        // Start Building A List
		glNewList(base + loop, GL_COMPILE);				
            // Use A Quad For Each Character
			glBegin(GL_QUADS);	
                glTexCoord2f(cx, cy + offsetT);             glVertex2i(0, 0);
                glTexCoord2f(cx + offsetS, cy + offsetT);   glVertex2i(pixelsWidth, 0);
                glTexCoord2f(cx + offsetS, cy);             glVertex2i(pixelsWidth, pixelsHeight);
                glTexCoord2f(cx, cy);                       glVertex2i(0, pixelsHeight);
            // Done Building Our Quad (Character)
			glEnd();									
            // Move To The Right Of The Character
			glTranslated(spacing, 0, 0);						
        // Done Building The Display List
		glEndList();									
	}  // for (UINT32 loop=0; loop<(rows * columns); loop++)													
}

GLTextureFont2D::~GLTextureFont2D(void)
{
    if(base != 0)
    {
        glDeleteLists(base, rows * columns);
    }
}

///////////////////////////////////////////////////////////////////////////////
// Other public methods
///////////////////////////////////////////////////////////////////////////////

void GLTextureFont2D::drawString(const Point &point, const string &msg) const
{
    // Select Our Font Texture
	glBindTexture(GL_TEXTURE_2D, name);	
    // Position The Text (0,0 - Bottom Left)
	glTranslated(point.getX(), point.getY(), 0);								
    // Update index of base list according to the offset specified in the
    // constructor
	glListBase(base - offset);						
    // Write The Text To The Screen
	glCallLists(msg.length(), GL_UNSIGNED_BYTE, msg.c_str());
}

///////////////////////////////////////////////////////////////////////////////
// Getters
///////////////////////////////////////////////////////////////////////////////



///////////////////////////////////////////////////////////////////////////////
// Setters
///////////////////////////////////////////////////////////////////////////////


